package com.loto.insurance.mis.web.controller.ajax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.mis.web.authority.INSAuthority;
import com.loto.insurance.mis.web.entity.Pager;
import com.loto.insurance.mis.web.entity.ajax.BaseJson;
import com.loto.insurance.mis.web.entity.ajax.GridJson;
import com.loto.insurance.mis.web.entity.ajax.UserInfo;
import com.loto.insurance.mis.web.enumeration.IsActiveEnum;
import com.loto.insurance.mis.web.enumeration.MenuEnum;
import com.loto.insurance.mis.web.enumeration.ResultTypeEnum;
import com.loto.insurance.mis.web.service.UserInfoService;
import com.loto.utils.DateUtils;
import com.loto.utils.MD5;

@Controller
public class UserJsonController {

  @Autowired
  UserInfoService userInfoService;

  @RequestMapping(value = "/login.json", method = RequestMethod.POST)
  public ModelAndView doLogin(HttpServletRequest request,
	  HttpServletResponse response,
	  @RequestParam(value = "name", required = false) String loginName,
	  @RequestParam(value = "pwd", required = false) String loginPwd) {
	ModelAndView mav = new ModelAndView();
	// List<GoodsInfo> goodsList = goodsInfoService.findUnPurchased(67001);
	BaseJson base = new BaseJson();
	// 判断验证码

	// 验证用户名和密码
	base = userInfoService.doLogin(request, loginName, loginPwd);
	mav.addObject("result", base);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.USER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/user/list.json", method = RequestMethod.POST)
  public ModelAndView useromerlist(
	  @RequestParam(value = "userName", required = false) String userName,
	  @RequestParam(value = "page", required = false, defaultValue = "1") int page,
	  @RequestParam(value = "rows", required = false, defaultValue = "20") int rows) {
	Map<String, Object> params = new HashMap<String, Object>();
	if (userName != null)
	  params.put("userName", userName);
	Pager pager = new Pager();
	pager.setPageSize(rows);

	params.put("pageSize", pager.getPageSize());
	params.put("startIndex", pager.getPageSize() * (page - 1));

	if (page > 1)
	  pager.setIndex(page);
	List<com.loto.insurance.mis.web.entity.UserInfo> userList = userInfoService
	    .findAllUser(params);

	List<UserInfo> list = new ArrayList<UserInfo>();
	for (com.loto.insurance.mis.web.entity.UserInfo user : userList) {
	  UserInfo u = new UserInfo();
	  u.setUserId(user.getUserId());
	  u.setUserName(user.getUserName());
	  u.setUserCreatedOn(DateUtils.formatShortDateTime(user.getUserCreatedOn()));
	  u.setUserRealName(user.getUserRealName());
	  if (user.getUserLastLoginOn() != null)
		u.setUserLastLoginOn(DateUtils.formatShortDateTime(user
		    .getUserLastLoginOn()));

	  int userStatus = user.getUserIsActive();
	  for (IsActiveEnum e : IsActiveEnum.values()) {
		if (e.getIndex() == userStatus) {
		  u.setUserIsActive(e.getName());
		  break;
		}
	  }

	  list.add(u);
	}

	pager.setRowCount(userInfoService.findAllUserCount(params));

	ModelAndView mav = new ModelAndView();

	GridJson<UserInfo> gridJson = new GridJson<UserInfo>();
	gridJson.setTotal(pager.getRowCount());
	gridJson.setRows(list);

	mav.addObject("result", gridJson);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.USER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/user/add.json", method = RequestMethod.POST)
  public ModelAndView useromeradd(
	  @RequestParam(value = "name", required = true) String userName,
	  @RequestParam(value = "realname", required = true) String userRealName,
	  @RequestParam(value = "pwd", required = true) String userPwd,
	  @RequestParam(value = "status", required = true) int userIsActive,
	  @RequestParam(value = "menulist[]", required = true) ArrayList<String> menulist) {
	com.loto.insurance.mis.web.entity.UserInfo userInfo = new com.loto.insurance.mis.web.entity.UserInfo();

	userInfo.setUserName(userName);
	userInfo.setUserIsActive(userIsActive);
	userInfo.setUserRealName(userRealName);
	userInfo.setUserPwd(MD5.GetMD5Code(userPwd));

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base.setOk(userInfoService.addUser(userInfo, menulist));
	mav.addObject("result", base);
	return mav;
  }

  @INSAuthority(authorityMenu = MenuEnum.USER_LIST, resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/user/modify.json", method = RequestMethod.POST)
  public ModelAndView useromermodify(
	  @RequestParam(value = "id", required = true) long userId,
	  @RequestParam(value = "realname", required = true) String userRealName,
	  @RequestParam(value = "pwd", required = false) String userPwd,
	  @RequestParam(value = "status", required = true) int userIsActive,
	  @RequestParam(value = "menulist[]", required = true) ArrayList<String> menulist) {

	com.loto.insurance.mis.web.entity.UserInfo userInfo = new com.loto.insurance.mis.web.entity.UserInfo();
	userInfo.setUserId(userId);
	userInfo.setUserIsActive(userIsActive);
	userInfo.setUserRealName(userRealName);
	if (userPwd != null && !userPwd.isEmpty())
	  userInfo.setUserPwd(MD5.GetMD5Code(userPwd));

	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base.setOk(userInfoService.updateUser(userInfo, menulist));
	mav.addObject("result", base);
	return mav;
  }
}
